﻿using System;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Text;
using Gqq.CodeFirst.Palace;
using System.Data.Entity;

namespace Gqq.CodeFirst.ProcessPalace
{
    public class Part8 : PartBase
    {
        public  static void DoTest()
        {
            TestCreate();
        }

        public static void TestProxyDisabled()
        {
            using (var context = new UnicornsContext())
            {
                var unicorns = context.Unicorns;
                //当ProxyCreationEnabled被设置成false的时候，
                //如果使用这个方法，可以将Princess取出来，不然的话，princess属性将为null
                unicorns.Include(x => x.Princess).Load();
                var info = unicorns.Find(6);

                var princess = db.Princesses.Find(19);
                Console.WriteLine(princess.GetType().FullName);

                // 要得到类型，一般使用ObjectContext.GetObjectType(princess.GetType())方法。
                Console.WriteLine(ObjectContext.GetObjectType(princess.GetType()).FullName);
            }

        }

        /// <summary>
        /// proxy instance will not be created if you create an instance of an entity using the new operator.
        ///  This may not be a problem, but if you need to create a proxy instance 
        /// (for example, so that lazy loading or proxy change tracking will work) 
        /// then you can do so using the Create method of DbSet
        /// </summary>
        public static void TestCreate()
        {
            var uni = db.Unicorns.Find(6);
            var newUni = db.Unicorns.Create();
            var newUni2 = new Unicorn();

            var entityType = ObjectContext.GetObjectType(newUni.GetType());
        }
    }
}
